<?php

class SubcategoriasModel extends Subcategorias{

	public function insertSubcategorias($arr){
		try {
			$obj = new Subcategorias();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met($valor);
				}
			}
			$db = new Db;
			$db->Insert('subcategorias',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function updateSubcategorias($arr){
		try {
			$obj = new Subcategorias();
			foreach($arr as $chave => $valor){
				if($chave != 'files'){
					$chave = ucfirst($chave);
					$met = 'set'.$chave;
					$obj->$met("'".$valor."'");
				}
			}
			$db = new Db;
			$db->Update('subcategorias',$obj);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function deleteSubcategorias($id){
		try {
			$db = new Db;
			$db->Delete('subcategorias',$id);
			$this->lista = $db->res;
		} catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function consultarSubcategorias($parametros){
		try {
			$campos = array('*');
			$where = 'idsubcategorias = '.$parametros;
			$order = '';
			$db = new Db;
			$db->Select('subcategorias',$campos,$where,$order);
			$this->lista = $db->res[0];
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function listarSubcategorias($parametros){
		try {
			$campos = array('*','c.nome AS nome_cat','s.nome AS nome_sub');
			$where = '';
			$order = 'c.idcategorias,c.nome,s.nome ASC';
			$db = new Db;
			$db->Select('subcategorias AS s INNER JOIN categorias as c ON c.idcategorias = s.idcategorias',$campos,$where,$order);
			$view = new SubcategoriasView();
			$this->lista = $view->listar($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function visualizarSubcategorias($parametros){
		try {
			$campos = array('*','c.nome AS nome_cat_1','s.nome_1 AS nome_sub_1');
			$where = 'c.idcategorias = '.$parametros;
			$order = 's.idsubcategorias ASC';
			$db = new Db;
			$db->Select('categorias as c LEFT JOIN subcategorias AS s ON c.idcategorias = s.idcategorias',$campos,$where,$order);
			$this->lista = $db->res;
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function dropdownSubcategorias(){
		try {
			$campos = array('*','c.nome AS nome_cat','s.nome AS nome_sub');
			$where = '';
			$order = 'c.idcategorias,c.nome,s.nome ASC';
			$db = new Db;
			$db->Select('subcategorias AS s INNER JOIN categorias as c ON c.idcategorias = s.idcategorias',$campos,$where,$order);
			$view = new SubcategoriasView();
			$this->lista = $view->select($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
	public function menuSubcategorias(){
		try {
			$campos = array('*','c.nome AS nome_cat','s.nome AS nome_sub');
			$where = '';
			$order = 'c.idcategorias,c.nome,s.nome ASC';
			$db = new Db;
			$db->Select('subcategorias AS s INNER JOIN categorias as c ON c.idcategorias = s.idcategorias',$campos,$where,$order);
			$view = new SubcategoriasView();
			$this->lista = $view->menu($db->res);
		}catch (Exception $e) {
			echo 'Erro: ',  $e->getMessage();
		}
	}
}
?>